<?php
namespace app\admin\controller;

use app\admin\controller\User;
use app\api\controller\KbxOther;
use app\api\model\FreeGroup;
use app\api\controller\Common;
use app\api\model\FreeGroupMember;
use app\api\model\GroupToursLine as GT;
use app\api\model\GrouptourQrcode as GTQ;
use app\api\controller\GroupToursWxSoft;
use app\api\model\Standard as SD;
use think\Exception;
use think\Request;
use think\Cookie;
use think\Cache;
use think\Db;

class Grouptoursline extends User{
    //初始化
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    //列表
    public function index()
    {
        $post = $this->request->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post=Cookie::get('postlist');
        }else{
            //把条件写入缓存
            Cookie::set('postlist',$post);
        }
        $where['is_deleted'] = 0;
        if(isset($post['title'])){
            $where['g.title|t.item_num'] = ['like','%'.trim($post['title']).'%'];
        }
//        $total = db("group_tours_line")->where($where)->count();
        $list = db("group_tours_line g")
            ->join("city c",'c.id=g.city_id','left')
            ->join("product t",'t.id=g.trip_id','left')
            ->join("admin a",'a.id=g.admin_id','left')
            ->join("goods p",'p.goods_id=g.product_id','left')
            ->join("grouptour_qrcode qr",'qr.grouptour_id=g.line_id','left')
            ->field("g.line_id,g.title,g.img,g.days,g.end_days,g.sold_num,g.sort_num,g.show_sold,g.sold_num+g.show_sold as total_sold,g.is_use,g.trip_id,g.product_id,g.create_time,g.admin_id,c.name as city_name,a.nickname as admin_name,p.goods_status,p.goods_name as product_name,g.is_recommend,t.name as trip_name,g.hotel_unit_price,qr.id as qrcode_id,expire_time as qrcode_expire_time,g.is_free")
            ->where($where)
            ->order("g.is_recommend desc,g.sort_num desc,total_sold desc,g.line_id desc")
            ->paginate(10);
//        var_dump($list);exit;
        $this->assign("list",$list);
        $this->assign("wherelist",$post);
        //过期时间比较
        $this->assign("now_time",date('Y-m-d',strtotime('+1 day')));
        return $this->fetch();
    }
    //新增/修改
    public function edit()
    {
        $post = request()->post();
        $model = new GT();
        if(!empty($post)){
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['title', 'require', '标题不能为空'],
                ['city_id', 'require', '请选择城市'],
                ['product_id', 'require', '请选择商品'],
                ['trip_id', 'require', '请选择产品'],
                ['img', 'require', '请上传缩略图'],
                ['from_address_json', 'require', '请填写出发地'],
                ['to_address_json', 'require', '请填写目的地'],
                ['carType', 'require', '请选择车辆类型'],
                ['subType', 'require', '请选择用车类型'],
                ['carPrice', 'require', '请填写包车成本价'],
                ['person_price', 'require', '请填写单人售价'],
            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
            if(isset($post['id']) && !empty($post['id'])){
                //修改
                if(false === $model->allowField(true)->save($post,['line_id'=>$post['id']])) {
                    return $this->error('修改失败');
                } else {
                    //自动新增套餐
                    $line = db('group_tours_line')->find($post['id']);
                    if($line['auto_spec_flag'] < 1){
                        $res = (new GT())->autoCreateSpecsByLineId($post['id']);
                        if(!is_array($res)){
                            return $this->error($res);
                        }
                    }
                    return $this->success('修改成功','admin/grouptoursline/index');
                }
            }else{
                //新增
                $post['admin_id'] = Cookie::get('admin');
                $post['create_time'] = date("Y-m-d H:i:s");
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {
                    $line_id = db()->getLastInsID();
                    $res = (new GT())->autoCreateSpecsByLineId($line_id);
                    if(!is_array($res)){
                        return $this->error($res);
                    }
                    return $this->success('添加成功','admin/grouptoursline/index');
                }
            }

        }
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if(!empty($id)){
            $where['line_id'] = $id;
            $info = db("group_tours_line gt")
                ->join("goods g","gt.product_id=g.goods_id",'left')
                ->join("product t","gt.trip_id=t.id",'left')
                ->field("gt.*,g.goods_name,t.name as trip_name")
                ->where($where)
                ->find();
            if(!empty($info['from_address_json'])){
                $info['from_address_arr'] = json_decode($info['from_address_json'],true);
            }
            if(!empty($info['to_address_json'])){
                $info['to_address_arr'] = json_decode($info['to_address_json'],true);
            }
        }else{
            $info = [];
        }
        $city_where['type'] = "CITY";
        $city_where['deleted'] = 0;
        $city = Db("city")->where($city_where)->field("id,name")->select();
        $this->assign('city',$city);
        $this->assign('info',$info);
        //默认拼游规则
        $default_rules = '1，选择出行日期参加拼游，以上没有可选日期则可以自己发起拼游。
2，一人保证出行，最多五位拼友，体验更好。
3，拼友们将根据人数惊喜返现，拼友越多返现越多。';
        $this->assign('default_rules',$default_rules);
        return $this->fetch();
    }
    //自由拼 新增/修改
    public function editFree()
    {
        $post = request()->post();
        $model = new GT();
        if(!empty($post)){
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['title', 'require', '标题不能为空'],
                ['city_id', 'require', '请选择城市'],
                ['img', 'require', '请上传缩略图']
            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
            if(isset($post['id']) && !empty($post['id'])){
                //修改
                if(false === $model->allowField(true)->save($post,['line_id'=>$post['id']])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改成功','admin/grouptoursline/index');
                }
            }else{
                //新增
                $post['is_free'] = 1;//自由拼
                $post['admin_id'] = Cookie::get('admin');
                $post['create_time'] = date("Y-m-d H:i:s");
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {
                    return $this->success('添加成功','admin/grouptoursline/index');
                }
            }

        }
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if(!empty($id)){
            $where['line_id'] = $id;
            $info = db("group_tours_line gt")
                ->join("goods g","gt.product_id=g.goods_id",'left')
                ->join("product t","gt.trip_id=t.id",'left')
                ->field("gt.*,g.goods_name,t.name as trip_name")
                ->where($where)
                ->find();
            if(!empty($info['from_address_json'])){
                $info['from_address_arr'] = json_decode($info['from_address_json'],true);
            }
            if(!empty($info['to_address_json'])){
                $info['to_address_arr'] = json_decode($info['to_address_json'],true);
            }
        }else{
            $info = [];
        }
        $city_where['type'] = "CITY";
        $city_where['deleted'] = 0;
        $city = Db("city")->where($city_where)->field("id,name")->select();
        $this->assign('city',$city);
        $this->assign('info',$info);
        return $this->fetch();
    }
    //修改展示状态
    public function changeUse()
    {
        $post = request()->post();
        if(!isset($post['id']) || !isset($post['is_use'])){
            $this->error("参数有误");exit;
        }
        $res = db("group_tours_line")->where(['line_id'=>$post['id']])->update(['is_use'=>$post['is_use']]);
        if($res!==false){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    //修改展示状态
    public function changeRecommend()
    {
        $post = request()->post();
        if(!isset($post['id']) || !isset($post['is_recommend'])){
            $this->error("参数有误");exit;
        }
        $res = db("group_tours_line")->where(['line_id'=>$post['id']])->update(['is_recommend'=>$post['is_recommend']]);
        if($res!==false){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    //修改权重
    public function changeSortNum()
    {
        $post = request()->post();
        if(!isset($post['id']) || !isset($post['sort_num'])){
            $this->error("参数有误");exit;
        }
        $res = db("group_tours_line")->where(['line_id'=>$post['id']])->update(['sort_num'=>$post['sort_num']]);
        if($res!==false){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    //删除
    public function del()
    {
        $post = request()->post();
        if(!isset($post['id'])){
            $this->error("参数有误");exit;
        }
        $res = db("group_tours_line")->where(['line_id'=>$post['id']])->update(['is_deleted'=>1]);
        if($res!==false){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }

    //查询关联的商品
    public function searchProduct()
    {
        $post = request()->post();
        //展示的商品
        $where['show'] = 1;
        $list = [];
        if(!empty($post['key'])){
            $where['goods_name'] = ['like','%'.$post['key'].'%'];
        }else{
            $list = cache("group_search_goods_list");
        }

        $list = db("goods")->field("goods_id,goods_name")->where($where)->select();
        if(empty($post['key'])){
            cache("group_search_goods_list",$list,3600);
        }
        $this->success("seccess",'',$list);
    }

    //查询关联的产品
    public function searchTrip()
    {
        $post = request()->post();
        //产品=1
        $where['use_type'] = 1;
        if(!empty($post['key'])){
            $where['name'] = ['like','%'.$post['key'].'%'];
        }

        $list = db("product")->field("id,name")->where($where)->select();
        $this->success("seccess",'',$list);
    }
    
    //清除关联数据缓存
    public function reflushData()
    {
        Cache::clear("toursline_goods");
        Cache::clear("toursline_trip");

        $this->success("刷新成功");
    }

    public function getCache()
    {
        $from = "orderSystem";
        $goods_id = 23190;
        $trip_id = 434;
        $goods = cache::get($from."_toursline_goods_".$goods_id);
        $trip = cache::get($from."_toursline_trip_".$trip_id);
        echo "<pre>";
        var_dump($goods,$trip);exit;
        echo "</pre>";
    }

    public function getSummaryTrip()
    {
        $post = request()->post();
        $trip_id = $post['trip_id']??0;

        $model = new GT();
        $tripData = $model->getTripData($trip_id);
        if(!empty($tripData)){
            $lineSummary = $model->getTripSummary($tripData);
            $lineSummary = implode("\n",$lineSummary);
        }else{
            $lineSummary = "";
        }
        $this->success("获取成功",'',$lineSummary);
    }
    ##########################  拼游群设置   ##############################
    //列表
    public function qrcode()
    {
        $post = $this->request->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post=Cookie::get('postlist');
        }else{
            //把条件写入缓存
            Cookie::set('postlist',$post);
        }
        $where = [];
        if(isset($post['title'])){
            $where['g.id|g.title|gt.title|g.notice_email'] = ['like','%'.trim($post['title']).'%'];
        }
        //前期只维护一个群
        //增加滑雪群
        $where['g.id'] = ['in',[1,10]];

        $model = new GTQ();
        $list = $model->alias("g")
            ->join("group_tours_line gt",'gt.line_id=g.grouptour_id','left')
            ->join("admin a",'a.id=g.adminid','left')
            ->field("g.*,gt.title as line_name,a.nickname as admin_name")
            ->where($where)
            ->order('expire_time asc')
            ->paginate(10);
        $notice_time = date('Y-m-d',strtotime('+1 day'));
        foreach ($list as $key=>$value){
            $list[$key]['is_notice'] = 0;
            if(strtotime($value['expire_time']) <= strtotime($notice_time)){
                $list[$key]['is_notice'] = 1;
            }
        }
        $this->assign("list",$list);
        $this->assign("wherelist",$post);
        return $this->fetch();
    }

    //添加/修改
    public function addqrcode()
    {
        $post = request()->post();
        $model = new GTQ();
        if(!empty($post)){
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['title', 'require', '标题不能为空'],
                ['grouptour_id', 'require', '请选择拼游产品'],
                ['qrcode', 'require', '请上传群二维码'],
                ['notice_email', 'require', '请填写提醒邮箱'],
                ['expire_time', 'require', '请填写过期时间'],
            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
            if(isset($post['id']) && !empty($post['id'])){
                $post['update_time'] = date('Y-m-d H:i:s');
                //修改
                if(false == $model->allowField(true)->save($post,['id'=>$post['id']])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改成功','admin/grouptoursline/qrcode');
                }
            }else{
                //新增
                $post['adminid'] = Cookie::get('admin');
                $post['update_time'] = $post['create_time'] = date('Y-m-d H:i:s');
                if(false == $model->allowField(true)->save($post)) {
                    return $this->error('添加失败');
                } else {
                    return $this->success('添加成功','admin/grouptoursline/qrcode');
                }
            }

        }
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if(!empty($id)){
            $where['id'] = $id;
            $info = db("grouptour_qrcode gq")
                ->join("group_tours_line gtl","gtl.line_id=gq.grouptour_id",'left')
                ->field("gq.*,gtl.title as line_name")
                ->where($where)
                ->find();
        }else{
            $info = [];
        }
        $this->assign('info',$info);
        //获取拼团产品列表
        $tourlist = db('group_tours_line')->field('line_id,title')->select();
        $this->assign('tourlist',$tourlist);
        //关联的拼游产品信息
        $line_id = input('line_id',0);
        $lineInfo = db('group_tours_line')->find($line_id);
        if(empty($line_id) || empty($lineInfo)){
            $lineInfo = [];
        }
        $this->assign('lineInfo',$lineInfo);
        return $this->fetch();
    }

    //删除拼游的二维码
    public function delqrcode()
    {
        $post = request()->post();
        if(!isset($post['id']) || empty($post['id'])){
            return $this->error("参数有误");exit;
        }

        $qrcode = db("grouptour_qrcode")->find($post['id']);
        if(empty($qrcode)){
           return $this->error("二维码信息不存在");
        }
        $res = db("grouptour_qrcode")->delete($post['id']);
        if($res){
            return $this->success("删除成功");
        }else{
            return $this->error("删除失败");
        }
    }

    //拼游产品规格
    public function specs(){
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $table_name = ['拼团类型','价格','拼团人数','最小成拼人数','最小增长数'];
        $line = db('group_tours_line')->find($id);
        if(empty($id) || empty($line)) return $this->error('拼游线路不存在');

        $packages = db('grouptour_package')->where("grouptour_id",$id)->select();
        if($packages){
            foreach ($packages as $key=>$value){
                $packages[$key]['child'] = db('grouptour_specs')->where("package_id",$value['id'])->select();
            }
        }
        //是新增操作
        if ($this->request->isPost()) {
            //是提交操作
            $post = request()->post();
            $data = $post['data']??[];
            if(empty($data)) return $this->error('请先添加套餐配置');
            db::startTrans(); //启动事务
            try {
                $package_ids = array_column($data,'id');
                $package_ids = array_filter($package_ids);
                $need_del_package = db('grouptour_package')
                    ->where(['grouptour_id'=>$id,'id'=>['not in',$package_ids]])
                    ->select();
                if(!empty($need_del_package)){
                    $need_del_package_ids = array_column($need_del_package,'id');
                    db('grouptour_specs')->where(['package_id'=>['in',$need_del_package_ids]])->delete();
                    db('grouptour_package')->where(['id'=>['in',$need_del_package_ids]])->delete();
                }

                foreach ($data as $key=>$value){
                    if(!empty($value['id'])){
                        db('grouptour_package')
                            ->where('id',$value['id'])
                            ->update([
                                'name'=>$value['name'],
                                'updatetime'=>time()
                            ]);
                        $package_id = $value['id'];
                    }else{
                        db('grouptour_package')->insert([
                            'grouptour_id'=>$id,
                            'name'=>$value['name'],
                            'creattime'=>time(),
                            'updatetime'=>time()
                        ]);
                        $package_id = db()->getLastInsID();
                    }
                    if(empty($value['child'])) continue;
                    foreach ($value['child'] as $k=>$v){
                        if(!empty($v['id'])){
                            db('grouptour_specs')
                                ->where('id',$v['id'])
                                ->update([
                                    'package_id'=>$package_id,
                                    'name'=>$v['name'],
                                    'num'=>$v['num'],
                                    'price'=>$v['price'],
                                    'minorder_num'=>$v['minorder_num'],
                                    'inc_order_num'=>$v['inc_order_num'],
                                    'updatetime'=>time(),
                                ]);
                        }else{
                            db('grouptour_specs')->insert([
                                'package_id'=>$package_id,
                                'name'=>$v['name'],
                                'num'=>$v['num'],
                                'price'=>$v['price'],
                                'minorder_num'=>$v['minorder_num'],
                                'inc_order_num'=>$v['inc_order_num'],
                                'creattime'=>time(),
                                'updatetime'=>time(),
                            ]);
                        }
                    }
                }

                db::commit();
                return $this->success('保存成功');
            }catch (Exception $e){
                db::rollback();
                return $this->error($e->getMessage());
            }
        }
        $this->assign('packages',$packages);
        $this->assign('id',$id);
        $this->assign('line',$line);
        $this->assign('table_name',$table_name);
        return $this->fetch();
    }

    //删除套餐
    public function delPackage()
    {
        $post = request()->post();
        $id = $post['id']??0;
        $res = db('grouptour_package')->find($id);
        if(empty($id) || empty($res)) return $this->error('套餐不存在');
        $res = db('grouptour_package')->delete($id);
        if($res){
            db('grouptour_specs')->where('package_id',$id)->delete();
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
    //删除规格
    public function delSpec()
    {
        $post = request()->post();
        $id = $post['id']??0;
        $res = db('grouptour_specs')->find($id);
        if(empty($id) || empty($res)) return $this->error('规格不存在');
        $res = db('grouptour_specs')->delete($id);
        if($res){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');

        }
    }

    //通过包车类型/城市获取日包车派单价
    public function getCarPriceBySubTypeAndCity()
    {
        $post = request()->post();
        $cityId = $post['cityId']??0;//出发城市
        $carType = $post['carType']??0;//车辆类型
        $season = $post['season']??'idleSeason';//淡旺季 默认淡季
        $subType = $post['subType']??0;//包车类型

        $city = db('city')->find($cityId);
        if(empty($cityId) || empty($city)) return $this->error('请选择城市');
        if(empty($city['kbx_id'])) return $this->error('请联系管理员，补充该城市的kbx_id');
        if(empty($subType)) return $this->error('请选择包车类型');
        //转化成订单系统识别的类型
        $subType = (new SD())->getSubType($subType);
        $data = [
            'city'=>$city['kbx_id'],
            'carType'=>$carType
        ];
        $res = (new KbxOther())->getCarPriceBySubTypeAndCity($data);
        if(!empty($res['msg'])) return $this->error($res['msg']);
        $idleCarPrice = $res['idleSeason'][$subType]??0;
        $busyCarPrice = $res['busySeason'][$subType]??0;
        $result = [
            'currency'=>$res['currency'],
            'idleCarPrice'=>$idleCarPrice,
            'busyCarPrice'=>$busyCarPrice,
        ];
        return $this->success('success','',$result);

    }

    //创建淡旺季套餐
    public function createSpecsById()
    {
        $post = request()->post();
        $id = $post['id']??0;
        $line = db('group_tours_line')->find($id);
        if(empty($id) || empty($line)) return $this->error('拼团线路有误！');
        //创建套餐
        $res = (new GT())->createSpecsByLineId($line);
        if(!is_array($res)){
            return $this->error($res);
        }
        return $this->success('成功');
    }

    //只创建旺季套餐 淡季套餐必须存在
    public function createBusySpecsById()
    {
        $post = request()->post();
        $id = $post['id']??0;
        $line = db('group_tours_line')->find($id);
        if(empty($id) || empty($line)) return $this->error('拼团线路有误！');
        //创建套餐
        $res = (new GT())->createBusySpecsByLineId($line);
        if(!is_array($res)){
            return $this->error($res);
        }
        return $this->success('成功');
    }

    //-----------------------------------自由行拼游-------------------------------
    //自由行拼游列表
    public function freegroup()
    {
        $post = $this->request->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post = Cookie::get('postlist');
        }else{
            //把条件写入缓存
            Cookie::set('postlist',$post);
        }
        $where['g.is_deleted'] = 0;
        if(!empty($post['title'])){
            $where['g.free_title'] = ['like','%'.trim($post['title']).'%'];
        }
        if(!empty($post['city'])){
            $where['g.start_city|g.end_city'] = $post['city'];
        }
        if(!empty($post['status'])){
            $where['g.status'] = $post['status'];
        }
        $model = new FreeGroup();
        $list = $model->name("free_group g")
            ->join("city s",'s.id=g.start_city','left')
            ->join("city e",'e.id=g.end_city','left')
            ->join("group_tours_line gtl",'gtl.line_id=g.grouptour_id','left')
            ->join("member m",'m.member_id=g.owner_id','left')
            ->field('g.*,s.name as start_city_name,e.name as end_city_name,gtl.img,m.member_nickname')
            ->where($where)
            ->order("g.is_recommend desc,id desc")
            ->paginate(10);
        foreach ($list as $key=>$value){
            $list[$key]['status_title'] = (new GroupToursWxSoft())->getFreeGroupStatusTitle($value['status'],true);
            $list[$key]['start_city_name'] = $value['start_city_name']?:'其他';
            $list[$key]['end_city_name'] = $value['end_city_name']?:'其他';
        }
        $this->assign("list",$list);
        $this->assign("wherelist",$post);

        $statusList = [
            1=>'拼游中',
            2=>'已完成',
            10=>'已解散',
        ];
        $typeList = [
            1=>'拼游',
            2=>'车找人',
            3=>'人找车',
        ];
        $this->assign("statusList",$statusList);
        $this->assign("typeList",$typeList);
        $cityList = db('city')->where("type","CITY")->field("id,name")->select();
        $this->assign("cityList",$cityList);
        return $this->fetch();
    }
    //自由行拼游详情
    public function freegroupinfo()
    {
        $post = request()->post();
        if(!empty($post)){
            //提交修改
            $model = new FreeGroup();
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['free_title', 'require', '请填写主题名称'],
                ['start_city', 'require', '请选择出发地城市'],
                ['end_city', 'require', '请选择目的地城市'],
                ['grouptour_id', 'require', '请选择关联拼团产品'],
            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
            if(isset($post['id']) && !empty($post['id'])){
                //修改
                if(false === $model->allowField(true)->save($post,['id'=>$post['id']])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改成功');
                }
            }
        }
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $info = db('free_group')->find($id);
        if(empty($id) || empty($info)) return $this->error('未查询到拼游信息');
        $info['status_title'] = (new GroupToursWxSoft())->getFreeGroupStatusTitle($info['status'],true);
        $model = new FreeGroupMember();
        $joinMemberList = $model->name('free_group_member fgm')
            ->join('member m','fgm.member_id=m.member_id','left')
            ->field('fgm.*,m.member_nickname')
            ->where(['fgm.is_deleted'=>0,'fgm.free_group_id'=>$id])
            ->paginate(10);
        foreach ($joinMemberList as $key=>$value){
            $joinMemberList[$key]['status_title'] = (new GroupToursWxSoft())->getFreeGroupStatusTitle($value['status']);
        }
        $city = (new Common())->getSoftCityList();
        $this->assign('city',$city);
        $this->assign('info',$info);
        $this->assign('memberlist',$joinMemberList);

        //关联产品
        $tourlist = db('group_tours_line')->where(['is_deleted'=>0,'is_free'=>1])->select();
        $this->assign('tourlist',$tourlist);
        return $this->fetch();
    }
    //自由行拼游用户详情
    public function freegroupmemberinfo()
    {
        $post = request()->post();
        if(!empty($post)){
            $model = new FreeGroupMember();
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $validate = new \think\Validate([
                ['country_code', 'require', '请填写手机区号'],
                ['phone', 'require', '请填写手机号码'],
            ]);
            //验证部分数据合法性
            if (!$validate->check($post)) {
                $this->error('提交失败：' . $validate->getError());
            }
            if(isset($post['id']) && !empty($post['id'])){
                //修改
                if(false === $model->allowField(true)->save($post,['id'=>$post['id']])) {
                    return $this->error('修改失败');
                } else {
                    return $this->success('修改成功');
                }
            }
        }
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $info = db('free_group_member')->find($id);
        if(empty($id) || empty($info)) return $this->error('未查询到用户拼游信息');
        $this->assign('info',$info);
        return $this->fetch();
    }
    //推荐自由行拼游
    //修改展示状态
    public function changeFreeRecommend()
    {
        $post = request()->post();
        if(!isset($post['id']) || !isset($post['is_recommend'])){
            $this->error("参数有误");exit;
        }
        $res = db("free_group")->where(['id'=>$post['id']])->update(['is_recommend'=>$post['is_recommend']]);
        if($res!==false){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    //修改备注信息
    public function changeFreeRemark()
    {
        $post = request()->post();
        if(!isset($post['id']) || !isset($post['remark'])){
            $this->error("参数有误");exit;
        }
        $res = db("free_group")->where(['id'=>$post['id']])->update(['remark'=>$post['remark']]);
        if($res!==false){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    //修改自由行拼游状态
    //修改自由行用户拼游状态
    //删除自由行拼游
    public function delfreegroup()
    {
        $post = request()->post();
        if(!isset($post['id'])){
            $this->error("参数有误");exit;
        }
        $res = db("free_group")->where(['id'=>$post['id']])->update(['is_deleted'=>1]);
        if($res!==false){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }

    //拼游小程序反馈意见
    public function grouptourfeedbacklist()
    {
        $post = $this->request->post();
        //分页bug处理
        $temp_page=request()->param('page');
        if(isset($temp_page)){
            //条件从缓存取出
            $post = Cookie::get('postlist');
        }else{
            //把条件写入缓存
            Cookie::set('postlist',$post);
        }
        $where['is_deleted'] = 0;
        if(!empty($post['content'])){
            $where['feedback_content|phone|email'] = ['like','%'.trim($post['content']).'%'];
        }
        if(isset($post['status']) && $post['status']!=''){
            $where['status'] = $post['status'];
        }
        $model = new FreeGroup();
        $list = $model->name("wxsoft_grouptour_feedback")
            ->where($where)
            ->order("id desc")
            ->paginate(10);

        $this->assign('list',$list);
        $this->assign('wherelist',$post);
        return $this->fetch();
    }

    public function changefeedbackstatus()
    {
        $post = request()->post();
        if(!isset($post['id'])){
            $this->error("参数有误");exit;
        }
        $res = db("wxsoft_grouptour_feedback")->where(['id'=>$post['id']])->update(['status'=>$post['status']]);
        if($res!==false){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }

    public function delfeedback()
    {
        $post = request()->post();
        if(!isset($post['id'])){
            $this->error("参数有误");exit;
        }
        $res = db("wxsoft_grouptour_feedback")->where(['id'=>$post['id']])->update(['is_deleted'=>1]);
        if($res!==false){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }
}